<!DOCTYPE html>
<html>

<head>
    <title>img to base64</title>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
</head>

<body>
    <img id="showImg" />
    <input type="file" onchange="changeFile(event);" />
    <a href="" id="download" download="data.xlsx">下载</a>
</body>

</html>
<script type="text/javascript">
    const download = document.getElementById('download');

    function changeFile(event) {
        file = event.target.files[0];
        var a = new FileReader();
        a.readAsDataURL(file);
        a.onload = function (e) {
            console.log(e);
            const base64Url = e.target.result; //获取base64

            download.setAttribute('href', base64Url);
            //下面是测试得到的base64串能否正常使用：
            // document.getElementById('showImg').src = base64Url;
        }

    }
</script>